/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sasp_messages;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import sasp_protocol.Entity;

/**
 *
 * @author mauriello
 */
public class REQDataCommand extends SASPCommand {
    private int [] requestedIDs;
    private int [] ignoredIDs;
    private String ipAddress;

    public int[] getIgnoredIDs() {
        return ignoredIDs;
    }

    public void setIgnoredIDs(int[] ignoredIDs) {
        this.ignoredIDs = ignoredIDs;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public int[] getRequestedIDs() {
        return requestedIDs;
    }

    public void setRequestedIDs(int[] requestedIDs) {
        this.requestedIDs = requestedIDs;
    }

    public REQDataCommand( ) {
        super( );
        this.setType(REQ_DATA);
    }

    @Override
    public void updateCommandBody( ) {
        try {
            JSONObject body = new JSONObject();
            JSONArray array = new JSONArray();
            for(int i = 0; i < requestedIDs.length; i++) {
                JSONObject test = new JSONObject();
                test.put("EntityID", requestedIDs[i]);
                array.put(test);
            }
            body.put("RequestEntityList", array);

            JSONArray array2 = new JSONArray();
            for(int i = 0; i < ignoredIDs.length; i++) {
                JSONObject test = new JSONObject();
                test.put("EntityID", ignoredIDs[i]);
                array2.put(test);
            }
            body.put("IgnoreEntityList", array2);

            body.put("IPAddress", this.ipAddress);

            this.setCommandBody(body);
        } catch (JSONException ex) {
            Logger.getLogger(REQDataCommand.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public REQDataCommand( int _o, int _d, int _m, String _ip, int [] _req, int [] _ign ) {
        super( _o, _d, _m );
        this.setType(REQ_DATA);
        saspmanager.SASPManagerApp.addLine("RDC - Request Data Command Generated to Entity " + _d);
        requestedIDs = _req;
        ignoredIDs = _ign;

        try {
            JSONObject body = new JSONObject();
            JSONArray array = new JSONArray();
            for(int i = 0; i < requestedIDs.length; i++) {
                JSONObject test = new JSONObject();
                test.put("EntityID", requestedIDs[i]);
                array.put(test);
            }
            body.put("RequestEntityList", array);

            JSONArray array2 = new JSONArray();
            for(int i = 0; i < ignoredIDs.length; i++) {
                JSONObject test = new JSONObject();
                test.put("EntityID", ignoredIDs[i]);
                array2.put(test);
            }
            body.put("IgnoreEntityList", array2);

            body.put("IPAddress", _ip);
            this.setCommandBody(body);
        } catch (JSONException ex) {
            Logger.getLogger(REQDataCommand.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public REQDataCommand( String _s ) {
        super( _s );

        try {
            saspmanager.SASPManagerApp.addLine("RDC - Request Data Command Received");
            JSONArray array = this.getCommandBody().getJSONArray("RequestEntityList");
            requestedIDs = new int[array.length()];
            for( int i = 0; i < array.length(); i++ ) {
                requestedIDs[i] = array.getInt(i);
            }
            array = this.getCommandBody().getJSONArray("IgnoreEntityList");
            ignoredIDs = new int[array.length()];
            for( int i = 0; i < array.length(); i++ ) {
                ignoredIDs[i] = array.getInt(i);
            }

            this.ipAddress = this.getCommandBody().getString("IPAddress");
        } catch (JSONException ex) {
            Logger.getLogger(REQMasterReply.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    @Override
    public SASPReply execute() {
        saspmanager.SASPManagerApp.addLine("RDC - ACK Reply Generated");
        return null; //new ACKReply(this.getDestinationID(),this.getOriginatorID(),this.getMessageID());
    }
}
